package com.kingbook.api.rest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kingbook.common.utils.StringUtils;
import com.kingbook.core.annotations.ContainProperties;
import com.kingbook.core.auth.LoginManager;
import com.kingbook.entity.Customer;
import com.kingbook.entity.Partner;
import com.kingbook.services.CustomerService;
import lombok.extern.slf4j.Slf4j;
import com.kingbook.core.result.JSONResult;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

import static com.kingbook.core.auth.LoginManager.AUTH;

/**
 * @Author Bucky
 * @Date 2020-01-08 21:13:15
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("customer")
public class CustomerRest {

    @Autowired
    CustomerService customerService;

    @PutMapping
    @ContainProperties({"id", "name"})
    public JSONResult save(@RequestBody Map<String, Object> param, @RequestHeader(name = AUTH) String auth) {
        param.put("mid", LoginManager.getMid(auth));
        log.info("---->{}", param);
        return customerService.updateCustomer(param);
    }

    @DeleteMapping("{id}")
    public JSONResult remove(@PathVariable int id, @RequestHeader(name = AUTH) String auth) {
        int mid = LoginManager.getMid(auth);
        log.info("---->mid={} id={}", mid, id);
        return customerService.remove(id, mid);
    }

    @GetMapping("{id}")
    public JSONResult find(@PathVariable int id, @RequestHeader(name = AUTH) String auth) {
        int mid = LoginManager.getMid(auth);
        log.info("---->mid={} id={}", mid, id);
        return customerService.queryCustomer(id, mid);
    }

    @PostMapping("query")
    public JSONResult query(@RequestBody Map<String, Object> param, @RequestHeader(name = AUTH) String auth) {
        param.put("mid", LoginManager.getMid(auth));
        log.info("---->{}", param);
        JSONResult result = customerService.query(param);
        JSONArray array = result.getData();
        JSONArray newArray = new JSONArray();
        for (int i = 0; i < array.size(); i++) {
            JSONObject order = array.getJSONObject(i);
            JSONObject json = new JSONObject();
            json.put("id", order.getIntValue("id"));
            json.put("avatarUrl", order.getString("avatarUrl"));
            json.put("title", order.getString("name"));
            json.put("desc1", "手机: " + order.getString("phone1") + " " + order.getString("phone2") + " " + order.getString("phone3"));
            json.put("desc2", "公司: " + order.getString("company"));
            json.put("desc3", "邮箱: " + order.getString("email"));
            json.put("desc4", "地址: " + order.getString("address"));
            json.put("desc5", "备注: " + order.getString("remarks"));
            int workspaceCount = order.getIntValue("workspaceCount");
            int orderCount = order.getIntValue("orderCount");
            json.put("workspaceCount", workspaceCount);
            json.put("orderCount", orderCount);
            json.put("del", workspaceCount == 0 && orderCount == 0);
            newArray.add(json);
        }
        result.setData(newArray);
        return result;
    }

    @PostMapping("pages")
    @ContainProperties({"pageNo", "pageSize"})
    public JSONResult queryForPages(@RequestBody Map<String, Object> param, @RequestHeader(name = AUTH) String auth) {
        param.put("mid", LoginManager.getMid(auth));
        return customerService.queryForPages(param);
    }

}
